import sun.plugin.javascript.navig.Array;

import java.util.Arrays;

public class demo {
    public static int[] sortArray(int[] nums) {
        duipa(nums);
        return nums;
    }
    public static void duipa(int []arr){
        int end= arr.length-1;
        dadui(arr,end);
        while (end>0){
            swap(arr,0,end);
            //只需要向下调整
            end--;
            xiato(arr,end);
        }
    }
    private  static void xiato(int[]arr,int end) {

        int parten=0;
        int childer = parten * 2 + 1;
        while (childer <= end) {
            if (childer + 1 <= end&& arr[childer] < arr[childer + 1]) {
                childer++;
            }
            if (arr[parten] < arr[childer]) {
                swap(arr, parten, childer);
                parten = childer;
                childer = parten * 2 + 1;
            } else {
                break;
            }
        }
    }
    private static void swap(int[]arr,int ret1,int ret2){
        int temp=arr[ret2];
        arr[ret2]=arr[ret1];
        arr[ret1]=temp;
    }
    private static void dadui(int []arr,int end){
        int usize= end;
        for (int parten=(usize-1)/2;parten>=0;parten--){
            int childer=parten*2+1;
            while(childer<=usize){
                if(childer+1<=usize&&arr[childer]<arr[childer+1]){
                    childer++;
                }
                if(arr[parten]<arr[childer]){
                    swap(arr,parten,childer);
                    parten=childer;
                    childer=parten*2+1;
                }
                else{
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        int []arr={5,2,3,1};
        sortArray(arr);
        System.out.println(Arrays.toString(arr));
    }
}
